package com.gla.market.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.gla.market.R;
import com.gla.market.entity.Commodity;
import com.gla.market.entity.ShopItem;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

public class AfterSearchAdapter extends BaseAdapter {

    private Context mContext;

    //传过来的数据
    private List<ShopItem> shopItemList;
    private Double La,Lo;

    public AfterSearchAdapter(Context mContext, List<ShopItem> shopItemList,Double La,Double Lo){
        this.shopItemList = shopItemList;
        this.mContext = mContext;
        this.La = La;
        this.Lo = Lo;
    }

    @Override
    public int getCount() {
        return shopItemList != null ? shopItemList.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return shopItemList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.shop_item, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        //数据初始化
        final ShopItem shopItem = shopItemList.get(position);
        if(!"".equals(shopItem.getPhoto())){
            ImageLoader.getInstance().displayImage(shopItem.getPhoto(), holder.searchShopPhoto);
        }
        String name = shopItem.getName();
        if(name.length() > 6) name =  name.substring(0,6) + "...";
        holder.shopItemName.setText(name);
        int distantPlace = shopItem.howfarm(Lo,La);
        String text = String.valueOf(distantPlace) +"m";
        holder.searchItemPlace.setText(text);

        return convertView;
    }
    class ViewHolder {
        ImageView searchShopPhoto;
        TextView shopItemName,searchItemPlace;
        public ViewHolder(View itemView) {
           searchShopPhoto = itemView.findViewById(R.id.shop_item_photo);
           shopItemName = itemView.findViewById(R.id.shop_item_name);
           searchItemPlace = itemView.findViewById(R.id.search_item_place);

        }
    }
}
